package com.lineage.list;

import com.lineage.config.Config;
import com.lineage.echo.ClientExecutor;
import com.lineage.server.datatables.lock.AccountReading;
import com.lineage.server.templates.L1Account;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class OnlineUser {
  private static final Log _log = LogFactory.getLog(OnlineUser.class);
  
  private static OnlineUser _instance;
  
  private final Map<String, ClientExecutor> _clientList = new ConcurrentHashMap<>();
  
  public static OnlineUser get() {
    if (_instance == null)
      _instance = new OnlineUser(); 
    return _instance;
  }
  
  public boolean addClient(L1Account value, ClientExecutor client) {
    String accountName = value.get_login();
    ClientExecutor xclient = this._clientList.get(accountName);
    if (xclient == null) {
      client.setAccount(value);
      value.set_isLoad(true);
      value.set_server_no(Config.SERVERNO);
      AccountReading.get().updateLan(accountName, true);
      this._clientList.put(accountName, client);
      return true;
    } 
    xclient.kick();
    client.kick();
    _log.error("连线列表中重复资料: " + value.get_login() + "\n");
    return false;
  }
  
  public boolean updateClient(L1Account value, ClientExecutor client) {
    String accountName = value.get_login();
    ClientExecutor xclient = this._clientList.get(accountName);
    if (xclient != null) {
      client.setAccount(value);
      value.set_isLoad(true);
      value.set_server_no(Config.SERVERNO);
      AccountReading.get().updateLan(accountName, true);
      this._clientList.put(accountName, client);
      _log.info("帐号登入资料更新: " + value.get_login() + " 目前连线帐号: " + this._clientList.size());
      return true;
    } 
    _log.error("连线列表中找不到资料: " + value.get_login() + "\n");
    return false;
  }
  
  public boolean isLan(String accountName) {
    ClientExecutor client = this._clientList.get(accountName);
    return (client != null);
  }
  
  public void remove(String accountName) {
    ClientExecutor xclient = this._clientList.get(accountName);
    if (xclient != null) {
      L1Account value = xclient.getAccount();
      value.set_isLoad(false);
      value.set_server_no(0);
      value.set_server_no(0);
      AccountReading.get().updateLan(accountName, false);
      this._clientList.remove(accountName);
      _log.info("\n       目前连线帐号: " + this._clientList.size() + "\n");
    } 
  }
  
  public ClientExecutor get(String accountName) {
    ClientExecutor client = this._clientList.get(accountName);
    return client;
  }
  
  public Map<String, ClientExecutor> map() {
    return this._clientList;
  }
  
  public Collection<ClientExecutor> all() {
    try {
      return Collections.unmodifiableCollection(this._clientList.values());
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
      return null;
    } 
  }
  
  public Collection<String> getObject() {
    return Collections.unmodifiableCollection(this._clientList.keySet());
  }
  
  public boolean isMax() {
    return (this._clientList.size() >= Config.MAX_ONLINE_USERS);
  }
  
  public int size() {
    return this._clientList.size();
  }
  
  public void kickAll() {
    Iterator<String> iterator = this._clientList.keySet().iterator();
    while (iterator.hasNext()) {
      String acc = iterator.next();
      remove(acc);
    } 
  }
}
